package com.cookman.renderers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Disposable;
import com.cookman.handlers.CustomGestureHandler;
import com.cookman.handlers.CustomInputProcessor;
import com.cookman.utils.Constants;

/**
 * Created by Cookman on 27.02.2015.
 */
public class BaseRenderer implements Disposable {

    //base camera, looks on game world
    public static OrthographicCamera camera;
    //deck camera, looks on different game decks
    public static OrthographicCamera cameraDeck;
    public static OrthographicCamera cameraGUI;
    public static OrthographicCamera cameraFPS;

    public static Stage stage;
    public static Stage stageDeck;

    protected SpriteBatch batch;
    protected SpriteBatch batchGUI;
    protected ShapeRenderer shapeRenderer;

    public static float ratew;
    public static float rateh;

    public static float uratew;
    public static float urateh;

    protected InputMultiplexer inputMultiplexer;
    protected CustomInputProcessor inputProcessor;
    protected CustomGestureHandler gestureHandler;

    public float zoom = 1.0f;

    @Override
    public void dispose() {
        batch.dispose();
    }

    public void updateZoom(float zoom) {
        this.zoom = zoom;
    }

    public void resize(int width, int height) {
        init();
    }

    protected void init() {

        batch = new SpriteBatch();
        batchGUI = new SpriteBatch();
        shapeRenderer = new ShapeRenderer();
    }

    protected void initaliseInputProcessors() {
        inputMultiplexer = new InputMultiplexer();
        inputProcessor = new CustomInputProcessor(camera, this);
        gestureHandler = new CustomGestureHandler(this);
        inputMultiplexer.addProcessor(new GestureDetector(gestureHandler));
        inputMultiplexer.addProcessor(inputProcessor);
        inputMultiplexer.addProcessor(stage);
        inputMultiplexer.addProcessor(stageDeck);
        Gdx.input.setInputProcessor(inputMultiplexer);
    }

    protected void moveCamera() {
        if (Gdx.input.isKeyPressed(Input.Keys.A)) {
            float delta = -0.02f;
            camera.zoom += delta;
            camera.update();
            cameraGUI.zoom += delta;
            cameraGUI.update();
        }

        if (Gdx.input.isKeyPressed(Input.Keys.Z)) {
            float delta = 0.02f;
            camera.zoom += delta;
            camera.update();
            cameraGUI.zoom += delta;
            cameraGUI.update();
        }

        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            camera.translate(0.05f, 0);
            camera.update();
            cameraGUI.translate(ratew * 0.05f, 0);
            cameraGUI.update();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            camera.translate(-0.05f, 0);
            camera.update();
            cameraGUI.translate(ratew * -0.05f, 0);
            cameraGUI.update();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            camera.translate(0, 0.05f);
            camera.update();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            camera.translate(0, -0.05f);
            camera.update();
        }

        float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
        float effectiveViewportHeight = camera.viewportHeight * camera.zoom;

        camera.zoom = MathUtils.clamp(camera.zoom, 0.05f, Constants.VIEWPORT_WIDTH / camera.viewportWidth);
        camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, Constants.VIEWPORT_WIDTH - effectiveViewportWidth / 2f);
        camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, Constants.VIEWPORT_HEIGHT - effectiveViewportHeight / 2f);
    }

    public void render(float deltaTime){
        ratew = cameraGUI.viewportWidth / camera.viewportWidth;
        rateh = cameraGUI.viewportHeight / camera.viewportHeight;
        uratew = camera.viewportWidth / cameraGUI.viewportWidth;
        urateh = camera.viewportHeight / cameraGUI.viewportHeight;
        moveCamera();
        camera.zoom = zoom;
        camera.update();
        cameraGUI.zoom = zoom;
        cameraGUI.update();

    }

}
